package com.wushi.lenovo.asia5b.app_ui.shopcar.adapter;

import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.mvp.utils.ResString;
import com.wushi.lenovo.asia5b.R;
import com.wushi.lenovo.asia5b.app_model.good.ShopModel;
import com.wushi.lenovo.asia5b.app_ui.shopcar.listener.ShopCarListener;

import java.util.List;

/**
 * Created by zhangyuncai on 2020/3/2.
 * 购物车店铺UI,包含若干商品
 */
public class ShopCarShopAdapter extends BaseQuickAdapter<ShopModel, BaseViewHolder> {
    private final ShopCarListener shopCarListener;


    public ShopCarShopAdapter(@Nullable List<ShopModel> data, ShopCarListener shopCarListener) {
        super(R.layout.item_shopcar_shopadapter, data);
        this.shopCarListener = shopCarListener;
    }

    @Override
    protected void convert(BaseViewHolder helper, ShopModel item) {
        //复选框
        ImageView iv_shopcarshop_check = helper.getView(R.id.iv_shopcarshop_check);
        iv_shopcarshop_check.setSelected(item.isChecked());
        iv_shopcarshop_check.setOnClickListener(v -> {
            item.setChecked(!v.isSelected());
            shopCarListener.selectGoods(item.products);
        });

        //店铺icon
        ImageView iv_shopcar_shopicon = helper.getView(R.id.iv_shopcar_shopicon);
        iv_shopcar_shopicon.setSelected(!shopCarListener.isDaigouGoods());

        if (shopCarListener.isDaigouGoods()) {//只有代购商品才有中国物流价格
            //中国物流价格
            TextView tv_shopcarshop_fee = helper.getView(R.id.tv_shopcarshop_fee);
            tv_shopcarshop_fee.setText(String.format(ResString.get(R.string.shopcar_fee), item.domestic_fee_text));
            //价格<=0隐藏
            helper.setGone(R.id.ll_shopcarshop_fee, item.domestic_fee > 0);
        }


        if (item.products != null && item.products.size() > 0) {
            //店铺名
            helper.setText(R.id.tv_shopcarshop_shopname, item.shop_name);

            //商品列表
            RecyclerView recyclerView = helper.getView(R.id.rv_shopcarshop);
            ShopCarPiGoodsAdapter goodsAdapter = new ShopCarPiGoodsAdapter(item, shopCarListener);
            recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
            recyclerView.setAdapter(goodsAdapter);
        }

    }
}
